package com.sangjie.souyidai.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sangjie.souyidai.https.Souyidai;
import com.sangjie.souyidai.service.SignService;

@Controller
public class ExecuteController {

	private Souyidai souyidai;
	private SignService signService;
	
	public SignService getSignService() {
		return signService;
	}

	public void setSignService(SignService signService) {
		this.signService = signService;
	}

	@RequestMapping("/execute.opt")
	public String execute(HttpServletRequest request, HttpServletResponse response, String bid, Integer amount) {
		
		if (StringUtils.isBlank(bid) || amount == null || amount <= 0) {
			return "error";
		}
		
		souyidai = new Souyidai();
		
		souyidai.bid(bid, amount);
		
		return "result";
	}
	
	@RequestMapping("/cancel.opt")
	public String cancel(HttpServletRequest request, HttpServletResponse response) {
		if (souyidai != null) {
			souyidai.cancel();
			souyidai = null;
			return "cancel_result";
		}
		return "cancel_error";
	}
	
	@RequestMapping("/sign.opt")
	public String signIn(HttpServletRequest request, HttpServletResponse response) {
		signService.signIn();
		return "result";
	}
	
	public String cancelSign(HttpServletRequest request, HttpServletResponse response) {
		signService.cancelSign();
		return "cancel_result";
	}
}
